package cn.cs.service.impl;

import cn.cs.client.ProductClient;
import cn.cs.common.*;
import cn.cs.pojo.AgentShop;
import cn.cs.pojo.CsParts;
import cn.cs.pojo.Product;
import cn.cs.service.ProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductClient productClient;

    public Page<Product> queryProductPageByMap(Map<String,Object> param, Integer pageNo, Integer pageSize)throws Exception{
        Integer total = productClient.getProductCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Product> productList = productClient.getProductListPageByMap(param);
        page.setRows(productList);
        return page;
    }

    @Override
    public Dto getProInfoById(String proId,String type) throws Exception{
        if("1".equals(type)){
            List<String> strs = productClient.getPlatFromProImages(proId);
            List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
            Map<String,Object> pro = new HashMap<String,Object>();
            for(String s:strs){
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("product_id",proId);
                map.put("product_img_url",s);
                list.add(map);
            }
            pro.put("imgs",list);
            Product product = new Product();
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("product",product);
            map.put("count",0);
            map.put("imgs",list);
            map.put("lbImgs",null);
            map.put("shop",null);
            map.put("proList",null);
            return DtoUtil.returnDataSuccess(map);
        }else{
            Product pro =  productClient.getProductById(proId);
            if(!EmptyUtils.isEmpty(pro)){
                Map<String,Object> map = new HashMap<String, Object>();
                map.put("product",pro);
                int n = productClient.getCellCount(proId);
                map.put("count",n);
                List<Map<String,Object>> images = productClient.getProImagesByProId(pro.getId());
                List<Map<String,Object>> infoImg = new ArrayList<Map<String, Object>>();
                List<Map<String,Object>> lbimg = new ArrayList<Map<String, Object>>();
                for(Map<String,Object> ims : images){
                    if(ims.get("img_type").equals("1")){
                        lbimg.add(ims);
                    }else{
                        infoImg.add(ims);
                    }
                }
                map.put("imgs",infoImg);
                map.put("lbImgs",lbimg);
                List<Map<String,Object>> evaluates = productClient.getProEvaluatesByProId(pro.getId());
                map.put("evaluates",evaluates);
                AgentShop shop = productClient.getShopById(pro.getShopId());
                map.put("shop",shop);
                Map<String,Object> param =new HashMap<String,Object>();
                param.put("shopId",pro.getShopId());
                param.put("productState","1");
                List<Product> proList = productClient.getProductListByMap(param);
                map.put("proList",proList);
                return DtoUtil.returnDataSuccess(map);
            }else{
                return DtoUtil.returnFail("此商品不存在","002");
            }
        }
    }

    @Override
    public Dto showProduct(HttpServletRequest request) throws Exception {
        String pageNum = request.getParameter("pageNum");
        Map<String,Object> param = new HashMap<String,Object>();
        if(EmptyUtils.isNotEmpty(pageNum)){
            param.put("begNum",(Integer.valueOf(pageNum)-1)*10);param.put("endNum",Integer.valueOf(pageNum)*10);
        }
        List<CsParts> list = productClient.showProduct(param);
        if(!EmptyUtils.isEmpty(list)){
            return DtoUtil.returnDataSuccess(list);
        }else{
            return DtoUtil.returnFail("暂无商品","002");
        }
    }



    @Override
    public Dto getAgentShopById(HttpServletRequest request) throws Exception{
        String id = request.getParameter("shopId");
        AgentShop shop = productClient.getShopById(id);
        if(EmptyUtils.isNotEmpty(shop)){
            Map<String,Object> param =new HashMap<String,Object>();
            param.put("shopId",id);
            param.put("productState","1");
            List<Product> list = productClient.getProductListByMap(param);
            if(EmptyUtils.isNotEmpty(list)){
                for(Product pro:list){
                    int n = productClient.getCellCount(pro.getId());
                    pro.setCount(n+"");
                }
                shop.setProducts(list);
            }
            return   DtoUtil.returnDataSuccess(shop);
        }else{
            return   DtoUtil.returnFail("没有此商铺","002");
        }
    }

    @Override
    public Dto getShopPro(HttpServletRequest request) throws Exception {
        Map<String,Object> map = new HashMap<String,Object>();
        String id = request.getParameter("shopId");
        String pageNum = request.getParameter("pageNum");
        String sales = request.getParameter("sales");
        String typeId = request.getParameter("typeId");
        String name = request.getParameter("name");
        if(EmptyUtils.isNotEmpty(sales)){
            map.put("sales","1");
        }
        if(EmptyUtils.isNotEmpty(typeId)){
            map.put("product_type_id",typeId);
        }
        if(EmptyUtils.isNotEmpty(name)){
            map.put("name","%"+name+"%");
        }
        map.put("shopId",id);
        map.put("begNum",(Integer.valueOf(pageNum)-1)*10);
        map.put("endNum",Integer.valueOf(pageNum)*10);
        List<Map<String,Object>> list =productClient.getShopPro(map);
        if(EmptyUtils.isNotEmpty(list)){
            for(Map<String,Object> pro:list){
                if(EmptyUtils.isNotEmpty(productClient.getCellCount(pro.get("id").toString()))){
                    pro.put("count",productClient.getCellCount(pro.get("id").toString())+"");
                }else{
                    pro.put("count","0");
                }
            }
            return DtoUtil.returnDataSuccess(list);
        }else{
            return DtoUtil.returnFail("没有找到相关商铺产品","002");
        }
    }
}
